package com.gulimall.member.service.impl;

import com.gulimall.common.utils.ServiceResponse;
import com.gulimall.coupon.api.client.CouponClient;
import com.gulimall.coupon.api.dto.CouponInDTO;
import com.gulimall.member.api.dto.MemberInDTO;
import com.gulimall.member.entity.MemberEntity;
import com.gulimall.member.mapper.MemberMapper;
import com.gulimall.member.service.MemberService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;

@Slf4j
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private CouponClient couponClient;

    @Autowired
    private MemberMapper memberMapper;

    @GlobalTransactional
    public ServiceResponse<Void> save(MemberInDTO memberVO) {

        MemberEntity memberEntity = new MemberEntity();
        BeanUtils.copyProperties(memberVO, memberEntity);
        memberMapper.insert(memberEntity);

        log.info("member信息已入库");

        // 构造优惠券信息
        log.info("Coupon服务调用结束");
        CouponInDTO couponInDTO = new CouponInDTO();
        couponInDTO.setCouponType(1);
        couponInDTO.setCouponImg("http://test.coupon.image/coupon.jpg");
        couponInDTO.setCouponName("100元减10元");
        couponInDTO.setNum(2);
        couponInDTO.setAmount(new BigDecimal(10L));
        couponInDTO.setPerLimit(3);
        couponInDTO.setMinPoint(new BigDecimal(100L));
        couponInDTO.setEndTime(LocalDate.now());
        couponInDTO.setUseType(3);
        couponInDTO.setNote("测试用");
        couponInDTO.setPublishCount(25);
        couponInDTO.setUseCount(10);
        couponInDTO.setReceiveCount(20);
        couponInDTO.setEnableEndTime(LocalDate.now());
        couponInDTO.setEnableStartTime(LocalDate.now());
        couponInDTO.setCode("tucddio");
        couponInDTO.setMemberLevel(1);
        couponInDTO.setPublish(1);

        couponClient.save(couponInDTO);
        log.info("Coupon服务调用结束");

        return new ServiceResponse<>();
        //throw new NullPointerException("抛出了异常");
    }
}
